package com.yanqu.dao;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.data.entity.GoodsGet;
import com.yanqu.data.entity.UserGoodsGetData;
import com.yanqu.jdbc.JdbcTemplateHelper;
import org.springframework.jdbc.support.rowset.SqlRowSet;

import java.sql.SQLException;
import java.sql.Types;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserGoodsGetDataDaoImpl extends TempDataObjectDao<UserGoodsGetData> {

    public static String getUserGoodsGetDataSql = "select * from `t_m_user_get_goods`";
    public Map<Long, UserGoodsGetData> getUserGoodsGetData() {

        Map<Long, UserGoodsGetData> result = new ConcurrentHashMap<>();
        try {
            SqlRowSet sqlRowSet = JdbcTemplateHelper.getJdbcTemplate().queryForRowSet(getUserGoodsGetDataSql);
            while (sqlRowSet.next()) {
                UserGoodsGetData data = parse(sqlRowSet);
                result.put(data.getUserId(), data);
            }
        }catch (Exception e){
            throw new RuntimeException(e);
        }
        return result;
    }

    public static String updateUserGoodsGetDataSql = "update `t_m_user_get_goods` set vip=?,lastResetTime=?,goodsGetListMap=? where userId=?";
    @Override
    protected boolean updateData(UserGoodsGetData userGoodsGetData) {
        userGoodsGetData.setBeginWriteTime();
        int[] types = new int[]{Types.INTEGER, Types.BIGINT, Types.VARCHAR, Types.BIGINT};
        Object[] values = new Object[4];
        int i = 0;
        values[i++] = userGoodsGetData.getVip();
        values[i++] = userGoodsGetData.getLastResetTime();
        values[i++] = JSON.toJSONString(userGoodsGetData.getGoodsGetListMap());
        values[i++] = userGoodsGetData.getUserId();
        if (JdbcTemplateHelper.getJdbcTemplate().update(updateUserGoodsGetDataSql, values, types) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserGoodsGetDataSql = "insert into `t_m_user_get_goods`(userId,vip,lastResetTime,goodsGetListMap) values (?,?,?,?)";
    @Override
    protected boolean addData(UserGoodsGetData userGoodsGetData) {
        userGoodsGetData.setBeginWriteTime();
        int[] types = new int[]{Types.BIGINT, Types.INTEGER, Types.BIGINT, Types.VARCHAR};
        Object[] values = new Object[4];
        int i = 0;
        values[i++] = userGoodsGetData.getUserId();
        values[i++] = userGoodsGetData.getVip();
        values[i++] = userGoodsGetData.getLastResetTime();
        values[i++] = JSON.toJSONString(userGoodsGetData.getGoodsGetListMap());
        if (JdbcTemplateHelper.getJdbcTemplate().update(insertUserGoodsGetDataSql, values, types) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserGoodsGetData parseData(SqlRowSet rs) throws SQLException {
        UserGoodsGetData userGoodsGetData = new UserGoodsGetData();
        userGoodsGetData.setUserId(rs.getLong("userId"));
        userGoodsGetData.setVip(rs.getInt("vip"));
        userGoodsGetData.setLastResetTime(rs.getLong("lastResetTime"));
        userGoodsGetData.setGoodsGetListMap(JSON.parseObject(rs.getString("goodsGetListMap"), new TypeReference<ConcurrentHashMap<Integer, List<GoodsGet>>>() {}));

        return userGoodsGetData;
    }
}
